package com.ybkj.o2o.zhuanxian.manager.impl;

import com.ybkj.o2o.zhuanxian.common.DataGrid;
import com.ybkj.o2o.zhuanxian.entity.Order;
import com.ybkj.o2o.zhuanxian.entity.OrderReview;
import com.ybkj.o2o.zhuanxian.manager.OrderReviewManager;
import com.ybkj.o2o.zhuanxian.service.OrderReviewService;
import com.ybkj.o2o.zhuanxian.service.OrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DefaultOrderReviewManager implements OrderReviewManager {

  @Autowired
  private OrderReviewService orderReviewService;
  @Autowired
  private OrderService orderService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int insert(OrderReview orderReview) {
    Order order = orderService.findByIdAndAppKey(orderReview.getOrderId(), orderReview.getAppKey());
    order.setReview(true);
    order.setEmployScore(orderReview.getScore());
    orderService.updateIngoreNull(order);
    return orderReviewService.insert(orderReview);

  }

  @Override
  public OrderReview findByOrderIdAndAppKey(Long orderId, String appKey) {

    return orderReviewService.findByOrderIdAndAppKey(orderId, appKey);
  }

  @Override
  public void deleteById(Long id, String appKey) {
    orderReviewService.deleteById(id, appKey);
  }

  @Override
  public OrderReview findOne(Long id, String appKey) {
    return orderReviewService.findOne(id, appKey);
  }

  @Override
  public DataGrid<OrderReview> findByDriver(Long driverId, Integer page,
      Integer rows, String appKey) {
    
    Integer limit = null;
    if (page != null && rows != null) {
      limit = (page - 1) * rows;
    }
    Long total = orderReviewService.countByDriver(driverId, appKey);
    List<OrderReview> list = orderReviewService.findByDriver(driverId, limit, rows, appKey);
    DataGrid<OrderReview> data = new DataGrid<OrderReview>();
    data.setRows(list);
    data.setTotal(total);
    return data;
  }
}
